
import { TreeNode } from "@/基本数据结构/二叉树";

export function deleteNode(root: TreeNode | null, key: number): TreeNode | null {
    if (!root) return null
    if (key < root.val) {
        root.left = deleteNode(root.left, key)
    } else if (key > root.val) {
        root.right = deleteNode(root.right, key)
    } else {
        if (!root.left) return root.right
        if (!root.right) return root.left
        let p = root.right
        while (p.left) {
           p = p.left 
        }
        p.left = root.left
        return root.right
    }
    return root
};